/*
 * 包含多个通过发送mutation间接更新state的方法的对象
 */
import {
  reqAddress,
  reqHouseCateorys,
  reqHouse,
} from '../api'

import {
  RECEIVE_USER_INFO,
  RECEIVE_TOKEN,
  RECEIVE_ADDRESS,
  RECEIVE_CATEGORYS,
  RECEIVE_HOUSE,
} from './mutation-types'

export default {
  // 同步记录用户信息
  recordUser ({commit}, userInfo) {
    commit(RECEIVE_USER_INFO, {userInfo})
  },
  //同步获取token
  getToken ({commit}, token) {
    commit(RECEIVE_TOKEN, {token})
  },
  // 异步获取地址
  async reqAddress({commit, state}) {
    //发送异步请求
    const geohash = state.latitude + "," + state.longitude
    const result = await reqAddress(geohash)
    //接收一个mutation
    if (result.code===0) {
      const address = result.data
      commit(RECEIVE_ADDRESS, {address});
    }
  },
  // 异步获取房源分类列表
  async reqCategorys ({commit}) {
    //发送异步请求
    const result = await reqHouseCateorys()
    //接收一个mutation
    if (result.code===0) {
      const categorys = result.data
      commit(RECEIVE_CATEGORYS, {categorys});
    }
  },
  // 异步获取房源列表
  async reqHouse ({commit, state}) {
    //发送异步请求
    const {latitude, longitude} = state
    const result = await reqHouse(longitude, latitude)
    //接收一个mutation
    if (result.code===0) {
      const house = result.data
      commit(RECEIVE_HOUSE, {house});
    }
  },
}


